package com.yxj.lib_exo;

import android.view.MotionEvent;
import android.view.ViewConfiguration;

public class ExoPlayerClick {


    private float dy, movey;
    private long currentTime;
    private boolean isLongClick = false ;
    //每次触摸只触发一次
    private boolean onceLongClick = false ;

    /**
     * @param event
     */
    public void onTouchEvent(MotionEvent event){
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                dy = event.getRawY();
                isLongClick = false ;
                currentTime = System.currentTimeMillis();
                onceLongClick = true ;
                break;

            case MotionEvent.ACTION_MOVE:
                movey = event.getRawY() - dy;
                isLongClick = LongClick();
                break;

            case MotionEvent.ACTION_UP:
                isLongClick = LongClick();
                if (Math.abs(movey) <= ViewConfiguration.getTouchSlop()) {
                    if (exoOnClickListener != null) {
                        if (!isLongClick) {
                            exoOnClickListener.onClick();
                        }
                    }
                }
                break;
        }
    }


    /**
     * 长按事件
     * @return
     */
    private boolean LongClick() {
        boolean b = false ;
        if (Math.abs(movey) <= ViewConfiguration.getTouchSlop()) {
            if (exoOnClickListener != null) {
                if (System.currentTimeMillis() - currentTime >= 2 * 1000) {
                    if (onceLongClick){
                        exoOnClickListener.onLongClick();
                        onceLongClick = false;
                    }
                    b = true ;
                }
            }
        }
        return b ;
    }




    private ExoPlayerControl.ExoOnClickListener exoOnClickListener;
    public void setExoClickListener(ExoPlayerControl.ExoOnClickListener clickListener) {
        this.exoOnClickListener = clickListener;
    }

}
